﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WaterStopClip : BaseObj
{
    public GameObject m_Target;
    public GameObject m_Target2;
    public Transform m_FatherTarget;
    public Transform m_BigFather;
    public Animator animator1;
    public Animator animator2;
    bool CanOpen;
    [HideInInspector]
    public static bool OpenJiaZi = true;
    bool closed = true;

    float distanceTop;
    float distanceDown;
    float distanceLeft;
    float distanceRight;
    void Start()
    {
        base.Start();
        CanOpen = false;
        m_rigidbody.useGravity = false;
        m_rigidbody.isKinematic = true;
        m_collider.isTrigger = true;
        transform.SetParent(m_FatherTarget);
    }

    public override void OnInit()
    {
        base.OnInit();
        if (mUpTrasnform != null)
            distanceTop = mUpTrasnform.position.y;
        if (mDownTrasnform != null)
            distanceDown = mDownTrasnform.position.y;
        if (mLeftTrasnform != null)
            distanceLeft = mLeftTrasnform.position.x;
        if (mRightTrasnform != null)
            distanceRight = mRightTrasnform.position.x;
    }
    public override void OnHandleTrigger(Transform camera, NoloDeviceType handleType)
    {
        isHandle = true;
        if (!canCatsh) return;
        //if (closed) return;
        if (!OpenJiaZi) return;
        base.OnHandleTrigger(camera, handleType);
        transform.rotation = Quaternion.Euler(-90, 0, 0);
        isCatch = true;
        if (mUpTrasnform != null)
            distanceTop = mUpTrasnform.position.y;
        if (mDownTrasnform != null)
            distanceDown = mDownTrasnform.position.y;
        if (mLeftTrasnform != null)
            distanceLeft = mLeftTrasnform.position.x;
        if (mRightTrasnform != null)
            distanceRight = mRightTrasnform.position.x;
    }

    public override void OnReleseTrigger(NoloDeviceType handleType)
    {
        transform.rotation = Quaternion.Euler(-90, 0, 0);
        isHandle = false;
        isCatch = false;
        m_collider.isTrigger = true;
        closed = true;
        if (CanOpen)//在皮管处
        {
            Close();
        }
        else
        {
            animator2.Play("ClipClose2");
            if (RoundBottpmFlask.HaveWater)
            {
                RoundBottpmFlask.Instance.StartAction();
            }
        }
    }

    public override void OnHoldingTrigger(Ray newRay)
    {
        isHandle = true;
        if (!canCatsh) return;
        if (closed) return;
        if (!OpenJiaZi) return;
        Ray deviceRay = newRay;
        Vector3 hitPos;
        if (RayTriInsecPos(m_IntersectPlane[0], m_IntersectPlane[1], m_IntersectPlane[2], deviceRay, out hitPos))
        {
            transform.position = hitPos;
            if (transform.position.y <= distanceDown)
            {
                transform.position = new Vector3(transform.position.x, distanceDown, transform.position.z);
            }
            if (transform.position.x >= distanceRight)
            {
                transform.position = new Vector3(distanceRight, transform.position.y, transform.position.z);
            }
            if (transform.position.x <= distanceLeft)
            {
                transform.position = new Vector3(distanceLeft, transform.position.y, transform.position.z);
            }
            if (transform.position.y >= distanceTop)
            {
                transform.position = new Vector3(transform.position.x, distanceTop, transform.position.z);
            }

        }
    }

    public void Open()
    {
        animator1.Play("open");
        animator2.Play("ClipOpen");
        OpenJiaZi = true;
        m_collider.isTrigger = true;
        if (StepsManager.Instance.StepTwoNum[2])
        {
            StepsManager.Instance.StepThreeNum[1] = true;
            StepsManager.Instance.SetStepThree(1);
        }
        m_rigidbody.useGravity = false;
        m_rigidbody.isKinematic = true;
    }

    public void Close()
    {
        animator1.Play("close");
        animator2.Play("ClipClose");
        m_rigidbody.useGravity = false;
        m_rigidbody.isKinematic = true;
        m_collider.isTrigger = true;
        if (RubberStopperDoubleKo.Instance.m_InWater)
        {
            StepsManager.Instance.StepTwoNum[2] = true;
            StepsManager.Instance.SetStepTwo(2);
        }
        OpenJiaZi = false;
    }

    private void Update()
    {
        if (!RoundBottpmFlask.Instance.m_IsStart)
        {
            if (NoloVR.NoloVRInput.singleton.OnNoloLeftTouchDown() || NoloVR.NoloVRInput.singleton.OnNoloRightTouchDown())
            {
                if (isHandle)
                {
                    if (OpenJiaZi && !closed)
                    {
                        if (CanOpen)//在皮管处
                            Close();
                        else
                            animator2.Play("ClipClose2");
                    }
                    else
                    {
                        Open();
                    }
                    closed = !closed;
                }
            }
        }
    }

    public void SetStartMove()
    {
        transform.rotation = Quaternion.Euler(-90, 0, 0);
        m_rigidbody.useGravity = false;
        m_rigidbody.isKinematic = true;
        m_collider.isTrigger = true;
    }

    public void SetTarget(int num)
    {
        if (num == 1)
        {
            //transform.position = m_Target.transform.position;
        }
        else
        {
            transform.position = m_Target2.transform.position;
        }
    }

    public void SetStopMove()
    {
        OpenJiaZi = true;
        m_rigidbody.useGravity = true;
        m_rigidbody.isKinematic = false;
        m_collider.isTrigger = true;
        StepsManager.Instance.StepThreeNum[1] = true;
        StepsManager.Instance.SetStepThree(1);
    }

    private void LateUpdate()
    {
        if (Vector3.Distance(m_Target2.transform.position, transform.position) < 0.01f)
        {
            CanOpen = true;
        }
        else
        {
            CanOpen = false;
            m_rigidbody.useGravity = false;
            m_rigidbody.isKinematic = true;
        }

        if (RoundBottpmFlask.Instance.m_IsStart || !OpenJiaZi)
            canCatsh = false;
        else
            canCatsh = true;
    }

    //记录每个大步骤的初始状态
    #region
    public void SetBigStepTwo()
    {
        transform.position = m_Target2.transform.position;
        OpenJiaZi = true;
    }

    public void SetBigStepThree()
    {
        Open();
        Debug.LogWarning(RoundBottpmFlask.HaveWater);
        if (RoundBottpmFlask.HaveWater)
        {
            RoundBottpmFlask.Instance.StartAction();
        }
    }

    public void SetBigStepFour()
    {
        ResetPosition();
        OpenJiaZi = true;
    }

    public void SetBigStepFive()
    {

    }
    #endregion

    [HideInInspector]
    public bool m_OnRay;
    public override void OnRayEnter()
    {
        base.OnRayEnter();
        m_OnRay = true;
    }

    public override void OnRayExit()
    {
        base.OnRayExit();
        m_OnRay = false;
    }
}
